package com.rongke.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.rongke.commons.JsonResp;
import com.rongke.mapper.CouponMapper;
import com.rongke.model.Coupon;
import com.rongke.model.CouponPage;
import com.rongke.service.CouponService;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @CouponController
 * @优惠券Controller
 * @version : Ver 1.0
 */
@RestController
@RequestMapping(value="/api/coupon")
@Transactional
@CrossOrigin
public class CouponController {
    private Logger log = Logger.getLogger(this.getClass());
    @Autowired
    private CouponService couponService;

    /**
     * @添加优惠券
     * @param coupon
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/add", method = RequestMethod.POST)
    public JsonResp addCoupon(@RequestBody Coupon coupon){
        log.debug("添加优惠券");
        coupon.setGmtDatetime(new Date());
        couponService.insert(coupon);
        return JsonResp.ok(coupon);
    }

    /**
     * @修改优惠券
     * @param coupon
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/update", method = RequestMethod.POST)
    public JsonResp updateCoupon(@RequestBody Coupon coupon){
        log.debug("修改优惠券");
        couponService.updateById(coupon);
        return JsonResp.ok(coupon);
    }

    /**
     * 删除某优惠券
     * @param id
     * @return
     */
    @RequestMapping(value = "/delCoupon",method = RequestMethod.GET)
    public JsonResp delCoupon(Long id){
        log.debug("删除某优惠券");
        couponService.deleteById(id);
        return JsonResp.ok("删除优惠券成功");
    }
    /**
     * @根据id查找优惠券
     * @param id
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/selectOne", method = RequestMethod.GET)
    public JsonResp selectCoupon(Long id){
        log.debug("查找优惠券");
        Coupon coupon = couponService.selectById(id);
        return JsonResp.ok(coupon);
    }

    /**
     * @根据条件查找优惠券
     * @param coupon
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/selectByCondition", method = RequestMethod.POST)
    public JsonResp selectCouponByCondition(@RequestBody Coupon coupon){
        log.debug("根据条件查找优惠券");
        if(coupon==null)
            return JsonResp.toFail("参数不能为空");
        List<Coupon> couponList=couponService.selectByCondition(coupon);
        return JsonResp.ok(couponList);
    }

    /**
     * 后台获取所有优惠券列表
     * @param couponPage
     */
    @RequestMapping(value = "/fetchAllCouponsAdmin",method = RequestMethod.POST)
    public JsonResp fetchAllCouponsAdmin(@RequestBody CouponPage couponPage){
        log.debug("后台获取所有优惠券列表");
        Coupon coupon=couponPage.getCoupon();
        EntityWrapper<Coupon> ew=new EntityWrapper<>(coupon);
        //搜索
        if(null!=couponPage.getSearchText()){
            ew.like("title",couponPage.getSearchText());
        }
        ew.orderBy("gmt_datetime",false);
        Page<Coupon> cpPage=couponService.selectPage(couponPage.getPage(),ew);
        return JsonResp.ok(cpPage);
    }

    /**
     *获取分享优惠的优惠券列表
     * @return
     */
    @RequestMapping(value = "/getShareCoupons",method = RequestMethod.GET)
    public JsonResp getShareCoupons(){
        log.debug("获取分享优惠的优惠券列表");
        EntityWrapper<Coupon> ew=new EntityWrapper<>();
        //diff_tag  3 普通分享（给好友等） 4 优质分享（到朋友圈等）
        Integer[] share={3,4};
        ew.in("diff_tag",share);
        ew.orderBy("gmt_datetime",false);
        List<Coupon> shareCoupons=couponService.selectList(ew);
        return JsonResp.ok(shareCoupons);
    }
}
